package jp.sourceforge.akjrcp.subtitle;



import jp.sourceforge.akjrcp.ui.ImageCanvas;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE.SharedImages;
import org.eclipse.ui.part.ViewPart;



/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class SubtitleEditView extends ViewPart{
	public static Log log = LogFactory.getLog(SubtitleEditView.class);

	public static final String ID = SubtitleEditView.class.getName();

	private ImageCanvas imageCanvas;

	public static void makeButton(Composite parent,String imageID){
		 Button bt=new Button(parent,SWT.NULL);
		 bt.setImage(PlatformUI.getWorkbench().getSharedImages().getImage(
				 imageID));
	}
	public void createPartControl(Composite parent) {
		parent.setLayout(new GridLayout(8,false));
		makeButton(parent,SharedImages.IMG_OBJ_PROJECT);
		makeButton(parent,SharedImages.IMG_OBJ_PROJECT_CLOSED);
		makeButton(parent,SharedImages.IMG_OBJS_BKMRK_TSK);
		makeButton(parent,SharedImages.IMG_OPEN_MARKER);
		makeButton(parent,SharedImages.IMG_OBJS_TASK_TSK);		
		makeButton(parent,ISharedImages.IMG_DEF_VIEW);
		makeButton(parent,ISharedImages.IMG_OBJ_ELEMENT);
		makeButton(parent,ISharedImages.IMG_OBJ_FILE);
		
		
		makeButton(parent,ISharedImages.IMG_OBJ_FOLDER);
		makeButton(parent,ISharedImages.IMG_OBJS_ERROR_TSK);
		
		
		makeButton(parent,ISharedImages.IMG_OBJS_INFO_TSK);
		makeButton(parent,ISharedImages.IMG_OBJS_WARN_TSK);
		makeButton(parent,ISharedImages.IMG_TOOL_BACK);
		makeButton(parent,ISharedImages.IMG_TOOL_BACK_DISABLED);
		makeButton(parent,ISharedImages.IMG_TOOL_COPY);
		
		makeButton(parent,ISharedImages.IMG_TOOL_COPY_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_COPY_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_CUT);
		makeButton(parent,ISharedImages.IMG_TOOL_CUT_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_CUT_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_DELETE);
		makeButton(parent,ISharedImages.IMG_TOOL_DELETE_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_DELETE_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_FORWARD);
		makeButton(parent,ISharedImages.IMG_TOOL_FORWARD_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_FORWARD_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_NEW_WIZARD);
		makeButton(parent,ISharedImages.IMG_TOOL_NEW_WIZARD_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_NEW_WIZARD_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_PASTE);
		makeButton(parent,ISharedImages.IMG_TOOL_PASTE_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_PASTE_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_REDO);
		makeButton(parent,ISharedImages.IMG_TOOL_REDO_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_REDO_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_UNDO);
		makeButton(parent,ISharedImages.IMG_TOOL_UNDO_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_UNDO_HOVER);
		makeButton(parent,ISharedImages.IMG_TOOL_UP);
		makeButton(parent,ISharedImages.IMG_TOOL_UP_DISABLED);
		//makeButton(parent,ISharedImages.IMG_TOOL_UP_HOVER);
		//makeButton(parent,ISharedImages.);
		//makeButton(parent,ISharedImages.);
		/*
		
		Composite leftSide=new Composite(parent,SWT.NULL);
		GridData data1=new GridData(GridData.FILL_VERTICAL);
		data1.minimumWidth=364;
		leftSide.setLayoutData(data1);
		Composite rightSide=new Composite(parent,SWT.NULL);
		GridData data2=new GridData(GridData.FILL_BOTH);
		rightSide.setLayoutData(data2);
		
		
		leftSide.setLayout(new GridLayout(1,false));
		
		imageCanvas = new ImageCanvas(leftSide,SWT.NULL);
		
		GridData data3=new GridData(GridData.FILL_HORIZONTAL);
		data3.minimumWidth=320;
		data3.minimumHeight=256;//240+16;
		imageCanvas.setLayoutData(data3);
		
		
		TimeSpinner TimeSpinner=new TimeSpinner(leftSide,SWT.NONE);
		Button bt=new Button(leftSide,SWT.NULL);
		bt.setText("play");
		bt.addSelectionListener(new SelectionAdapter(){
			public void widgetSelected(SelectionEvent event){
				StartPaint p=new StartPaint();
				p.start();
			}
		});
		
		gc=new GC(imageCanvas);
		*/
	}
	GC gc;
	public class StartPaint extends Thread{
		public void run(){
			startPaint();
			//PlatformUI.getWorkbench().getDisplay().syncExec(new ThreadPaint());
			//startPaint();
		
		}
	}

	public class ThreadPaint extends Thread{
		public void run(){
			startPaint();
		}
	}
	protected  void startPaint() {
		long start=System.currentTimeMillis();
		
		for(int i=0;i<30*10;i++){
		
		Image image=getImage(i);
		
		
		gc.drawImage(image, 0, 0);
		//gc.drawImage(image, 0,0,320, 240,0,0,640,480);
		image.dispose();
		}
		//gc.dispose();
		long end=System.currentTimeMillis();
		double sec=((double)end-start)/1000;
		log.info(sec+" sec");
	}

	private static Image getImage(int i) {
		
		return new Image(null,"c:\\tmp\\imagegrey\\stream"+toDigits(i)+".jpg");
	}
	
	public static String toDigits(int value){
		String result=""+value;
		while(result.length()<5){
			result="0"+result;
		}
		return result;
	}
	
	public void setFocus() {
		
	}


}